A tool that measures where each frame's time goes, so you can find out why a game runs slow instead of guessing.
The GBA draws a new picture about 60 times a second. Everything your game does for one picture, every Step event, every move, every sprite drawn, has to fit inside one frame: 280,896 cycles of the processor, and not one more. If the work takes longer, the frame is late and the game drops to 30 pictures a second. The player sees that as slow, jerky movement.
When that happens, the hard part is knowing what is slow. Guessing is a trap. People have blamed the wrong thing for weeks, and spent that time making something faster that was never the problem. The profiler does the measuring for you.
The profiler splits each frame into 24 phases, the real jobs the game does every frame, in the order it does them. It reads the GBA's own clock at the start of each one. The phases never overlap and they add up to the whole frame. Every number is given as a percentage of one frame:
The profiler starts off, and a game built with it off is exactly the game it would have been if the profiler did not exist: not a byte bigger, not a cycle slower. Turn it on when you build:
python tools/gbabel_build.py <your project> --clean --profile--clean is needed the first time you switch, because it changes how the whole game is built. Leave the flag off for the version you give to players. It is for finding slow spots, not for shipping.
The profile_ calls hand the numbers to your game, so you can draw whatever you like. This is a complete overlay. Put it in the Draw GUI event of any object that is always in the room:
if (profile_enabled()) {
draw_text(4, 2, $"TOTAL {int_round(profile_average(PROFILE_TOTAL))}");
for (var i = 1; i <= profile_phases(); i++) {
var percent = int_round(profile_average(i));
if (percent > 0) {
draw_text(4, 2 + i * 6, $"{profile_name(i)} {percent}");
}
}
}The overlay is drawn by your game, so it costs time too, and that time is counted: it shows up in the draw gui phase. If you want an honest reading of a screen, look at it with the overlay on, then again with it off, and compare the totals.
There are three ways to ask for a phase's cost:
| Call | What it gives | Good for |
|---|---|---|
profile_percent | the last frame | catching what is happening right now |
profile_average | the last 32 frames | reading on screen, it holds still |
profile_worst | the worst frame so far | short spikes, like a room loading |
profile_cycles and profile_budget give the same measurement as raw processor cycles, if you would rather have those.
In the order a frame runs them.
| # | Phase | What the game is doing |
|---|---|---|
| 1 | room change | Fading, wiping and loading a new room |
| 2 | audio | Keeping music and sound effects going |
| 3 | lights | Moving the holes of light in a dark room |
| 4 | tile animation | Animated tiles, like water and torches |
| 5 | effects | Particles and other effects |
| 6 | cutscenes | Playing a cutscene |
| 7 | step begin | Your Step Begin events |
| 8 | alarms | Counting alarms down, and your Alarm events |
| 9 | button events | Checking every Button event, and running yours |
| 10 | step | Your Step events |
| 11 | motion | Moving every instance by its speed, direction, gravity and friction |
| 12 | paths | Instances following a path |
| 13 | room edges | Outside Room, Hit Room Edge and the view events |
| 14 | collision scan | Finding which pairs of objects overlap, for your Collision events |
| 15 | collision events | Running your Collision events |
| 16 | step end | Your Step End events |
| 17 | camera | Moving the camera |
| 18 | animation | Moving every sprite to its next picture, and your Animation End events |
| 19 | draw begin | Your Draw Begin events |
| 20 | default sprites | The game drawing every object's own sprite for you |
| 21 | draw | Your Draw and Draw End events |
| 22 | draw gui | Your Draw GUI events, and your overlay |
| 23 | cleanup | Tidying up unused drawing work |
| 24 | screen update | The GBA's graphics system moving every sprite, background and color into place |
Phase 24 is not timed directly. It is whatever the processor did in the frame that the other 23 phases do not account for. Sprites and backgrounds and tile layers cost their time here: the game does no work on a background every frame, so a tile layer shows up in this phase and not in one of its own.
The end-of-frame clean-up of destroyed instances is counted here too: it runs just after the screen has been updated, so the Destroy events of the instances destroyed in a frame are timed in phase 24 of the frame after it. If that phase is big and your screen is quiet, look at what your Destroy events do.
Your own code is counted in the phase that runs it. If your Step event calls pos_meeting fifty times, all fifty are in step, not in a collision phase. collision scan and collision events are only the Collision events you wrote. To find out how much of step is collision checks, change them and measure again. That is the whole method: change one thing, then measure.
Time is counted for every object at once. The profiler tells you the phase, not the object. For that, build with --profile=objects, which also writes a report to the emulator's log (see below).
A development build has the GBA's safety checks switched on. They stop the game with an error screen when something goes wrong, which is what you want while you make it, but each check costs a little time, and a game full of sprites runs a lot of them. A --release build switches them off. The same room can measure 10 to 20 percentage points lighter in a --release build, so a room that lags in your development build may not lag in the one you ship. Measure both:
python tools/gbabel_build.py <your project> --clean --profile --releaseA build with --profile=log writes a summary of every phase to the emulator's log about every two seconds, and one whenever the room changes. mGBA can run a game with no window and print that log:
mgba-sdl.exe -l 255 game.gba > log.txtThe lines that start with GBA Debug: GBP are the report. --profile=objects adds which object's code the time was spent in, and how many of each object there were. It is slower because it times each object separately, and the report says how much that adds.
It works in a finished (release) build too, and the report names every object. It tells apart up to 32 objects that run code in the same stretch of 120 frames; an object past those shares the last line. It needs about 3.5 KB of the console's main memory (out of 256 KB): if a game is already so full that this does not fit, the build stops and says how many bytes too many, instead of making a game that starts to a black screen. Use --profile=log then.
| Build | Cost |
|---|---|
| Off (the default) | Nothing. No code, no memory, no clock reads. The finished game is identical to one made without the profiler. |
--profile | About 2,100 cycles a frame, under 1% of a frame, and about 650 bytes of the GBA's fast memory (out of 32,768). |
--profile=log | The same, plus a little more for the report. |
--profile=objects | Noticeably more. Read it for which object, then check the total with --profile=log. |
A tool that slowed the thing it measures would be worse than none, so the numbers are worth reading only when they are small next to what they measure.